/*
 * @Author: your name
 * @Date: 2021-05-31 14:17:15
 * @LastEditTime: 2021-06-07 15:13:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-demo\app\model\goodsgroup.js
 */

'use strict';

// const goodsgroup = require('../schema/goodsgroup');

module.exports = app => {
  const { Sequelize, model } = app;
  const { Op } = Sequelize;
  const goodsGroupSchema = require('../schema/goods_groups')(app);
  const GoodsGroup = model.define('goods_groups', goodsGroupSchema,{ timestamps: false,  }); // 定义表model；

  GoodsGroup.saveNew = async goodsgroup => {
    const res = await GoodsGroup.create(goodsgroup);
    return res;
  };

  GoodsGroup.saveModify = async goodsgroup => {
    const { name, intro, price, imgs, uuid } = goodsgroup;
    const updateField = { name, intro, price, imgs };
    const res = await GoodsGroup.update(updateField, {where: { uuid }});
    return res;
  }

  GoodsGroup.queryList = async (pagenition = {},filter={}) => {
    const { page, pageSize } = pagenition;
    
    const condition = {
      offset: ((page - 1)*pageSize),
      limit: +pageSize,
      where: filter
    }

    const list =  await GoodsGroup.findAndCountAll(condition);
    return { page, pageSize, ...list };
  }

  GoodsGroup.get = async ({ uuid, attributes }) => {
    return await GoodsGroup.findByPk(uuid, { attributes})
  }

  GoodsGroup.getOne = async ({uuid}) => {
    return await GoodsGroup.findByPk(uuid)
  }

  return GoodsGroup;
}